<?php
/**
 * @Author: Marte
 * @Date:   2019-09-20 09:43:37
 * @Last Modified by:   Marte
 * @Last Modified time: 2019-12-16 17:15:13
 */

namespace Home\Controller;
use Home\Controller\CommunicationController;
use User\Api\UserApi;
/**
 * 文档模型控制器
 * 文档模型列表和详情
 */
class AppController extends HomeController {
	//首页
    public function index(){
        $uid=I('uid');
    	//banner图
    	$banner_list=M('banner')->select();
        foreach($banner_list as $k=>$v){
            $banner_list[$k]['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$v[img]")->getfield('path');
        }
    	//文字滚播 文章（没看到总后台有发布文章   我这里暂取news表  新闻促销）
    	$article_list=M('news')->select();
    	//热销楼盘
        $_POST['area']?$map['area']=array('like',"%$_POST[area]%"):'';
        // file_put_contents('512.txt',$_POST['area']);
        $_POST['name']?$map['name']=array('like',"%$_POST[name]%"):'';
    	$floor_list=M('floor')->where($map)->limit(0,2)->select();
        foreach($floor_list as $k=>$v){
            $floor_list[$k]['sell']=explode(',',$v['selling_point']);
            $floor_list[$k]['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$v[img]")->getfield('path');
            if($uid){
                $map['floor_id']=$v['id'];
                $map['uid']=$uid;
                $info=M('follow_floor')->where($map)->find();
                if($info){
                    //已关注
                    $floor_list[$k]['gz']=1;
                }else{
                    //未关注
                    $floor_list[$k]['gz']=2;
                }
            }else{
                //没登陆也是未关注
                $floor_list[$k]['gz']=2;
            }
        }
    	$banner_list?$data['banner_list']=$banner_list:$data['banner_list']=[];
    	$article_list?$data['article_list']=$article_list:$data['article_list']=[];
    	$floor_list?$data['floor_list']=$floor_list:$data['floor_list']=[];
    	return api_return(1,'成功',$data);
    }
    public function index1(){
        $uid=I('uid');
        //banner图
        $banner_list=M('banner')->select();
        foreach($banner_list as $k=>$v){
            $banner_list[$k]['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$v[img]")->getfield('path');
        }
        //文字滚播 文章（没看到总后台有发布文章   我这里暂取news表  新闻促销）
        $article_list=M('news')->select();
        //热销楼盘
        $_POST['city']?$map['city']=array('like',"%$_POST[city]%"):'';
        // file_put_contents('512.txt',$_POST['area']);
        $_POST['name']?$map['name']=array('like',"%$_POST[name]%"):'';
        $floor_list=M('floor')->where($map)->limit(0,2)->select();
        foreach($floor_list as $k=>$v){
            $floor_list[$k]['sell']=explode(',',$v['selling_point']);
            $floor_list[$k]['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$v[img]")->getfield('path');
            if($uid){
                $map['floor_id']=$v['id'];
                $map['uid']=$uid;
                $info=M('follow_floor')->where($map)->find();
                if($info){
                    //已关注
                    $floor_list[$k]['gz']=1;
                }else{
                    //未关注
                    $floor_list[$k]['gz']=2;
                }
            }else{
                //没登陆也是未关注
                $floor_list[$k]['gz']=2;
            }
        }
        $banner_list?$data['banner_list']=$banner_list:$data['banner_list']=[];
        $article_list?$data['article_list']=$article_list:$data['article_list']=[];
        $floor_list?$data['floor_list']=$floor_list:$data['floor_list']=[];
        return api_return(1,'成功',$data);
    }
    //楼盘详情
    public function floor_detail(){
        $id=I('id');
        if(!$id){
            return api_return(2,'楼盘ID不能为空');
        }
        $uid=I('uid');
        if($uid){
            //如果登录了，就保存浏览记录
            $add_data['uid']=$uid;
            $add_data['floor_id']=$id;
            $add_data['floor_name']=M('floor')->where("id=$id")->getField('name');
            $add_data['create_time']=time();
            $bbtime=date('Y-m-d',time());//今天
            $ebtime=date('Y-m-d',strtotime("+1 day"));//明天
            $btime=strtotime($bbtime);
            $etime=strtotime($ebtime);
            $condition['create_time']=array('between',"$btime,$etime");
            $condition['uid']=$uid;
            $condition['floor_id']=$id;
            $re=M('browse')->where($condition)->find();
            if($re){
                //今天有保存到这个浏览记录表，就更新
                M('browse')->where("id=$re[id]")->save($add_data);
            }else{
                M('browse')->add($add_data);
            }
        }
        $data['info']=M('floor')->where("id=$id")->find();
        if($data['info']['imgs']){
            $imgs=explode(',',$data['info']['imgs']);
            unset($data['info']['imgs']);
            // file_put_contents('123.txt', $imgs);
            foreach($imgs as $k=>$v){
                $data['info']['imgs'][$k]="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$v")->getfield('path');
            }
        }
        // print_r($data['info']['imgs']);die;
        if($data['info']['video']){
            $video=$data['info']['video'];
            $data['info']['video']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$video")->getfield('path');
        }
        if($data['info']['status']==1){
            $data['info']['status']='待售';
        }elseif($data['info']['status']==2){
            $data['info']['status']='在售';
        }elseif($data['info']['status']==3){
            $data['info']['status']='售罄';
        }
        $sell=explode(',', $data['info']['selling_point']);

        //沙盘图片
        $sand_img=$data['info']['sand_img'];
        if($sand_img){
            $data['info']['sand_img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$sand_img")->getField('path');
        }
        //围观人员
        $list=M('follow_floor a')->join('left join l_ucenter_member b on a.uid=b.id')->where("a.floor_id=$id")->field('a.*,b.img')->select();

        foreach($list as $k=>$v){
            if(is_numeric($v['img'])){
                //不是整型的话就是微信头像，是一个地址，不需要处理
                $list[$k]['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$v[img]")->getfield('path');
            }
        }

        //围观人员数量
        $data['count']=M('follow_floor')->where("floor_id=$id")->count();
        $list?$data['list']=$list:$data['list']=[];
        //置业顾问
        $staff_list=M('department_staff')->where("floor_id=$id && role=1")->limit(0,3)->select();
        foreach($staff_list as $k=>$v){
            if(is_numeric($v['img'])){
                //不是整型的话就是微信头像，是一个地址，不需要处理
                $staff_list[$k]['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$v[img]")->getfield('path');
            }
        }
        $uid=I('uid');
        if($uid){
            $condition['uid']=$uid;
            $condition['floor_id']=$id;
            $r=M('follow_floor')->where($condition)->find();
            if($r){
                $data['gz']=1;
            }else{
                $data['gz']=2;
            }
        }else{
            $data['gz']=2;
        }


        //楼栋
        $build_list=M('building')->where("floor_id=$id")->select();
        //楼栋详情 选第一个
        $build_info=M('building')->where("floor_id=$id")->order('id asc')->find();
        $build_info?$data['build_info']=$build_info:$data['build_info']='';
        //户型  选第一个 应该是 1#的户型
        // $data['house_type']=M('house_type')->where("build_id=$build_info[id]")->select();
        $house_type=explode(',', $build_info['sale_type']);
        // return api_return(1,'成功',$data['house_type']);

        foreach($house_type as $k=>$v){
            // unset($data['house_type']);
            $bb=M('house_type')->where("id=$v")->find();
            $data['house_type'][$k]['id']=$v;
            $data['house_type'][$k]['house_img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$bb[house_img]")->getfield('path');
            $data['house_type'][$k]['house_area']=$bb['house_area'];
            $data['house_type'][$k]['house_config']=$bb['house_config'];
            $data['house_type'][$k]['name']=$bb['name'];
        }
        // return api_return(1,'成功',$data);
        $build_list?$data['build_list']=$build_list:$data['build_list']=[];

        $staff_list?$data['staff_list']=$staff_list:$data['staff_list']=[];
        $data['house_type']?$data['house_type']=$data['house_type']:$data['house_type']=[];
        $sell?$data['sell']=$sell:$data['sell']='';
        return api_return(1,'成功',$data);
    }


    //户型  楼盘详情点击楼栋
    public function build_info(){
        $build_id=I('build_id');
        $data['build_info']=M('building')->where("id=$build_id")->find();

        // $data['house_type']=M('house_type')->where("build_id=$build_id")->select();
        // foreach($data['house_type'] as $k=>$v){
        //     $data['house_type'][$k]['house_img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$v[house_img]")->getfield('path');
        // }
        $sale_type=$data['build_info']['sale_type'];
        $house_type=explode(',', $sale_type);
        foreach($house_type as $k=>$v){
            $bb=M('house_type')->where("id=$v")->find();
            $data['house_type'][$k]['id']=$v;
            $data['house_type'][$k]['house_img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$bb[house_img]")->getfield('path');
            $data['house_type'][$k]['house_area']=$bb['house_area'];
            $data['house_type'][$k]['house_config']=$bb['house_config'];
            $data['house_type'][$k]['name']=$bb['name'];
        }


        return api_return(1,'成功',$data);
    }

    //自助购房
    public function self_help(){
        $uid=I('uid');

        //热销楼盘
        $_POST['city']?$map['city']=array('like',"%$_POST[city]%"):'';
        // file_put_contents('512.txt',$_POST['area']);
        $_POST['name']?$map['name']=array('like',"%$_POST[name]%"):'';
        $floor_list=M('floor')->where($map)->limit(0,2)->select();
        foreach($floor_list as $k=>$v){
            $floor_list[$k]['sell']=explode(',',$v['selling_point']);
            $floor_list[$k]['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$v[img]")->getfield('path');
            if($uid){
                $map['floor_id']=$v['id'];
                $map['uid']=$uid;
                $info=M('follow_floor')->where($map)->find();
                if($info){
                    //已关注
                    $floor_list[$k]['gz']=1;
                }else{
                    //未关注
                    $floor_list[$k]['gz']=2;
                }
            }else{
                //没登陆也是未关注
                $floor_list[$k]['gz']=2;
            }
        }

        $floor_list?$data['floor_list']=$floor_list:$data['floor_list']=[];
        return api_return(1,'成功',$data);
    }
    //关注楼盘
    public function follow_floor(){
        $floor_id=I('id');
        if(!$floor_id){
            return api_return(2,'楼盘不能为空');
        }
        $uid=I('uid');
        if(!$uid){
            return api_return(2,'未登录');
        }
        $map['floor_id']=$floor_id;
        $map['uid']=$uid;
        $info=M('follow_floor')->where($map)->find();

        if(!$info){
            $map['create_time']=time();
            $res=M('follow_floor')->add($map);
        }else{
            $id=$info['id'];
            $res=M('follow_floor')->where("id=$id")->delete();
        }
        if($res){
            return api_return(1,'成功');
        }else{
            return api_return(2,'失败');
        }
    }
    //取消关注   从我的页面里面，我的关注里面过来的，ID是follow_floor表的ID，跟上面关注楼盘不同
    public function qx_floow(){
        $id=I('id');
        if(!$id){
            return api_return(2,'对象ID不能为空');
        }
        $res=M('follow_floor')->where("id=$id")->delete();
        if($res){
            return api_return(1,'成功');
        }else{
            return api_return(2,'失败');
        }
    }
    //推荐客户
    public function recommended_customers(){
        $uid=I('uid');
        if(!$uid){
            return api_return(2,'未登录');
        }
        // file_put_contents('123.txt', $_POST);exit;
        // $data['floor_id']=$_POST['floor_id'];
        // if(!$_POST['floor_id']){
        //     return api_return(2,'楼盘ID不能为空');
        // }
        if($_POST['floor_id']){
            file_put_contents('456.txt', $_POST);
            $floor_id=$_POST['floor_id'];
            $data['floor_id']=$floor_id;
            $data['floor_name']=M('floor')->where("id=$floor_id")->getfield('name');


            $staff_id=$_POST['index456'];
            if(is_numeric($staff_id) === false){
                return api_return(2,'置业顾问不能为空');
            }
            $staff_list=M('department_staff')->where("role=1 && floor_id=$floor_id")->order("id desc")->select();
            $data['adviser']=$staff_list[$staff_id]['id'];
            $data['adviser_name']=$staff_list[$staff_id]['staff_name'];
        }else{

            $floor_id=$_POST['index123'];

            if(is_numeric($floor_id) === false){
                return api_return(2,'楼盘ID不能为空');
            }
            $floor_list=M('floor')->order("id desc")->select();
            $data['floor_id']=$floor_list[$floor_id]['id'];
            $data['floor_name']=$floor_list[$floor_id]['name'];


            $staff_id=$_POST['index456'];
            if(is_numeric($staff_id) === false){
                return api_return(2,'置业顾问不能为空');
            }
            $staff_list=M('department_staff')->where("role=1")->order("id desc")->select();
            $data['adviser']=$staff_list[$staff_id]['id'];
            $data['adviser_name']=$staff_list[$staff_id]['staff_name'];
        }


        $data['name']=$_POST['name'];
        if(!$_POST['name']){
            return api_return(2,'客户姓名不能为空');
        }
        // $data['sex']=$_POST['sex'];
        // if(!$_POST['sex']){
        //     return api_return(2,'性别不能为空');
        // }
        if($_POST['index']==1){
            $data['sex']=2;
        }else{
            $data['sex']=1;
        }
        $data['mobile']=$_POST['mobile'];
        if(!$_POST['mobile']){
            return api_return(2,'手机号码不能为空');
        }
        // $data['adviser']=$_POST['adviser'];
        // if(!$_POST['adviser']){
        //     return api_return(2,'对接的置业顾问不能为空');
        // }

        $data['remark']=$_POST['remark'];
        $data['source']=1;//来源 推荐
        $data['uid']=$uid;
        //被推荐人UID
        $b_uid=M('ucenter_member')->where("mobile=$data[mobile]")->getfield('id');
        //被推荐人没有登录过 就再登录的时候再去判断手机号码看有没有被推荐过   是不是要添加到ucenter_member表
        if($b_uid){
            $data['b_uid']=$b_uid;
        }else{
            $data['b_uid']='';
        }
        $res=M('user')->add($data);
        if($res){
            return api_return(1,'成功');
        }else{
            return api_return(2,'失败');
        }
    }
    //楼盘列表
    public function floor_list(){
        $uid=I('uid');
        if(!$uid){
            return api_return(2,'未登录');
        }
        $state=M('ucenter_member')->where("id=$uid")->getField('state');
        if($state!==1 || $state !==2){

            return api_return(2,'不是经纪人');

        }
        if($_POST['floor_id']){
            //有楼盘ID的话 楼盘不用选 置业顾问也是这个楼盘的置业顾问
            $info=M('floor')->where("id=$_POST[floor_id]")->find();

            $s_list=M('department_staff')->where("role=1 && floor_id=$_POST[floor_id]")->order('id desc')->select();
            $staff='';
            foreach($s_list as $k=>$v){
                if($k==0){
                    $staff=$v['staff_name'];
                }else{
                    $staff.=','.$v['staff_name'];
                } 
            }
            $data['floor']=$info;
            $data['staff_list']=explode(',',$staff);
        }else{
            $list=M('floor')->order('id desc')->select();
            $floor='';
            foreach($list as $k=>$v){
                if($k==0){
                    $floor=$v['name'];
                }else{
                    $floor.=','.$v['name'];
                } 
            }
            $s_list=M('department_staff')->where("role=1")->order('id desc')->select();
            $staff='';
            foreach($s_list as $k=>$v){
                if($k==0){
                    $staff=$v['staff_name'];
                }else{
                    $staff.=','.$v['staff_name'];
                } 
            }
            $data['floor']=explode(',', $floor);
            $data['staff_list']=explode(',',$staff);
        }

        return api_return(1,'成功',$data);
    }
    //预约看房
    public function inspection(){
        $uid=I('uid');
        if(!$uid){
            return api_return(2,'未登录');
        }

        $floor_id=I('floor_id');
        if(!$floor_id){
            return api_return(2,'对象ID不能为空');
        }

        $map['floor_id']=$floor_id;
        $map['uid']=$uid;
        $map['source']=3;
        $info=M('user')->where($map)->find();
        if($info){
            return api_return(2,'不能重复预约');
        }

        $data['name']=M('floor')->where("id=$floor_id")->getField('name');
        // $data['staff_list']=M('department_staff')->where("role=1 && floor_id=$floor_id")->select();

        $s_list=M('department_staff')->where("role=1 && floor_id=$floor_id")->order('id desc')->select();
        $staff='';
        foreach($s_list as $k=>$v){
            if($k==0){
                $staff=$v['staff_name'];
            }else{
                $staff.=','.$v['staff_name'];
            } 
        }
        $data['staff_list']=explode(',',$staff);


        return api_return(1,'成功',$data);
    }
    //预约看房 提交
    public function house_inspection(){
        $data['floor_id']=$_POST['floor_id'];
        if(!$_POST['floor_id']){
            return api_return(2,'楼盘ID不能为空');
        }
        $uid=$_POST['uid'];
        if(!$uid){
            return api_return(2,'未登录');
        }
        $data['floor_name']=M('floor')->where("id=$_POST[floor_id]")->getField('name');
        $data['name']=$_POST['name'];
        if(!$_POST['name']){
            return api_return(2,'客户姓名不能为空');
        }
        $data['mobile']=$_POST['mobile'];
        if(!$_POST['mobile']){
            return api_return(2,'手机号码不能为空');
        }
        $staff_id=$_POST['index456'];
        if(is_numeric($staff_id) === false){
            return api_return(2,'置业顾问不能为空');
        }
        $staff_list=M('department_staff')->where("role=1 && floor_id=$_POST[floor_id]")->order("id desc")->select();
        $data['adviser']=$staff_list[$staff_id]['id'];
        $data['adviser_name']=$staff_list[$staff_id]['staff_name'];
        $data['source']=3;//来源 预约
        $data['create_time']=time();
        $data['uid']=$uid;
        $res=M('user')->add($data);
        if($res){
            return api_return(1,'成功');
        }else{
            return api_return(2,'失败');
        }
    }
    //户型详情
    public function house_detail(){
        $id=I('id');
        if(!$id){
            return api_return(2,'对象ID不能为空');
        }
        $info=M('house_type')->where("id=$id")->find();
        $info['house_img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$info[house_img]")->getfield('path');
        //价格
        $price=M('floor')->where("id=$info[floor_id]")->getField('price');
        $info['price']=$price*$info['house_area']/10000;
        $info['hot_line']=M('floor')->where("id=$info[floor_id]")->getField('hot_line');

        //热销户型 ，推荐的是这个楼盘的户型
        // $condition['build_id']=$info['build_id'];
        $condition['floor_id']=$info['floor_id'];
        $condition['id']=array('neq',$info['id']);

        $list=M('house_type')->where($condition)->select();
        // print_r($list);die;
        foreach($list as $k=>$v){
            $list[$k]['house_img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$v[house_img]")->getfield('path');
        }
        $info['list']=$list;
        if($info){
            return api_return(1,'成功',$info);
        }else{
            return api_return(2,'暂无信息');
        }
    }
    //置业顾问 更多
    public function staff_more(){
        $id=I('id');//楼盘ID
        if(!$id){
            return api_return(2,'楼盘ID不能为空');
        }
        $list=M('department_staff')->where("floor_id=$id && role=1")->select();
        foreach($list as $k=>$v){
            if($v['img']){
                $list[$k]['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$v[img]")->getfield('path');
            }else{
                $list[$k]['img']='';
            }
        }
        if($list){
            return api_return(1,'成功',$list);
        }else{
            return api_return(1,'暂无信息',[]);
        }
    }
    //置业顾问详情
    public function staff_info(){
        $id=I('id');
        if(!$id){
            return api_return(2,'对象ID不能为空');
        }
        $info=M('department_staff')->where("id=$id")->find();
        if($info['img']){
            $info['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$info[img]")->getfield('path');
        }else{
            $info['img']='';
        }
        $zan=explode(',',$info['zan']);
        foreach($zan as $k=>$v){
            $bb=$k;
        }
        $info['zan']=$bb-1;

        $aa=$info['browse'];

        $browse=explode(',',$info['browse']);
        foreach($browse as $k=>$v){
            $bb=$k;
        }
        $info['browse']=$bb-1;


        //浏览人的头像
        $bb=ltrim($aa,',');
        $cc=rtrim($bb,',');
        $browse_img=explode(',',$cc);
        foreach($browse_img as $k=>$v){
            if($v){
                $browse_img[$k]="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$v")->getfield('path');
            }else{
                $browse_img[$k]='';
            }
            
        }
        // print_r($browse_img);die;
        $info['browse_img']=$browse_img;
        $uid=I('uid');
        if($uid){
            //增加浏览量
            $duid=','.$uid.',';
            $condition['id']=$id;
            // print_r($duid);die;
            $condition['browse']=array('like',"%$duid%");
            $in=M('department_staff')->where($condition)->find();
            if($in){
                //已存在就不用管
            }else{
                $bro=M('department_staff')->where("id=$id")->getField('browse');
                $bro .=$uid.',';
                M('department_staff')->where("id=$id")->save(['browse'=>$bro]);
            }            
        }
        

        return api_return(1,'成功',$info);
    }
    //点赞
    public function dz(){
        $uid=I('uid');
        if(!$uid){
            return api_return(2,'未登录');
        }
        $id=I('id');
        if(!$id){
            return api_return(2,'对象ID不能为空');
        }
        $duid=','.$uid.',';
        $condition['id']=$id;
        // print_r($duid);die;
        $condition['zan']=array('like',"%$duid%");
        $info=M('department_staff')->where($condition)->find();
        if($info){
            $zz=M('department_staff')->where("id=$id")->getField('zan');
            //return api_return('2','已点过赞');
            //取消赞
            $a=$duid;

            $zan=str_replace($a,',',$zz);
            $res=M('department_staff')->where("id=$id")->save(['zan'=>$zan]);
        }else{
            $dz=M('department_staff')->where("id=$id")->getField('zan');
            $dz .=$uid.',';
            $res=M('department_staff')->where("id=$id")->save(['zan'=>$dz]);
        }
        if($res){
            return api_return(1,'成功');
        }else{
            return api_return(2,'失败');
        }
    }
    //活动管理 促销活动 其实是机构端的活动管理，机构端的促销是新闻动态
    public function activity_index(){
        $uid=$_POST['uid'];
        $area=$_POST['area'];
        $_POST['title']?$condition['a.title']=$_POST['title']:'';
        $area?$condition['b.area']=$area:'';
        $list=M('activity a')->join('left join l_floor b on a.floor_id=b.id')->where($condition)->field('a.*')->select();
        foreach($list as $k=>$v){
            $list[$k]['start_time']=date('Y-m-d',$v['start_time']);
            $list[$k]['end_time']=date('Y-m-d',$v['end_time']);
            $list[$k]['bs_time']=date('Y-m-d',$v['bs_time']);
            $list[$k]['be_time']=date('Y-m-d',$v['be_time']);
            //报名人数
            $list[$k]['number']=M('sign_up')->where("activity_id=$v[id]")->count();
            //判断是否参与
            if($uid){
                $map['activity_id']=$v['id'];
                $map['uid']=$uid;
                $info=M('sign_up')->where($map)->find();
                if($info){
                    $list[$k]['is_cy']=1;//已参与
                }else{
                    $list[$k]['is_cy']=2;//未参与
                }
            }else{
                //未登录，默认未参与，点击立即参与去判断是否登录
                $list[$k]['is_cy']=2;//未参与
            }
            if($v['img']){
                $list[$k]['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$v[img]")->getfield('path');
            }else{
                $list[$k]['img']='';
            }
            $list[$k]['floor_name']=M('floor')->where("id=$v[floor_id]")->getField('name');
        }
        if($list){
            return api_return(1,'成功',$list);
        }else{
            return api_return(1,'暂无信息',[]);
        }
    }

    //活动详情
    public function activity_info(){
        $id=I('id');
        if(!$id){
            return api_return(2,'对象ID不能为空');
        }
        //活动详情
        $data['activity_info']=M('activity')->where("id=$id")->find();
        $data['activity_info']['start_time']=date('Y-m-d',$data['activity_info']['start_time']);
        $data['activity_info']['end_time']=date('Y-m-d',$data['activity_info']['end_time']);
        $data['activity_info']['bs_time']=date('Y-m-d',$data['activity_info']['bs_time']);
        $data['activity_info']['be_time']=date('Y-m-d',$data['activity_info']['be_time']);
        if($data['activity_info']['img']){
            $imgg=$data['activity_info']['img'];
            $data['activity_info']['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$imgg")->getfield('path');
        }else{
            $data['activity_info']['img']='';
        }

        $uid=I('uid');
        if($uid){
            $map['activity_id']=$id;
            $map['uid']=$uid;
            $info=M('sign_up')->where($map)->find();
            if($info){
                $data['activity_info']['is_cy']=1;//已参与
            }else{
                $data['activity_info']['is_cy']=2;//未参与
            }
        }else{
            //未登录，默认未参与，点击立即参与去判断是否登录
            $data['activity_info']['is_cy']=2;//未参与
        }



        //多少人报名
        $data['activity_info']['number']=M('sign_up')->where("activity_id=$id")->count();
        //楼盘信息
        $floor_id=$data['activity_info']['floor_id'];
        $data['floor_info']=M('floor')->where("id=$floor_id")->find();
        if($data['floor_info']['img']){
            $img=$data['floor_info']['img'];
            $data['floor_info']['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$img")->getfield('path');
        }else{
            $data['floor_info']['img']='';
        }
        $data['floor_info']['sell']=explode(',',$data['floor_info']['selling_point']);
        return api_return(1,'成功',$data);
    }
    //参与活动
    public function participate_in(){
        $uid=I('uid');
        if(!$uid){
            return api_return(2,'未登录');
        }
        $data['uid']=$uid;
        $id=I('id');
        if(!$id){
            return api_return(2,'对象ID不能为空');
        }
        $data['activity_id']=$id;
        $data['name']=I('name');
        if(!$data['name']){
            return api_return(2,'姓名不能为空');
        }
        $data['mobile']=I('mobile');
        if(!$data['mobile']){
            return api_return(2,'手机号码不能为空');
        }
        //自定义属性不知道是啥
        $data['custom_properties1']=I('custom_properties1');
        $data['custom_properties2']=I('custom_properties2');
        $data['custom_properties3']=I('custom_properties3');
        $data['create_time']=time();
        $res=M('sign_up')->add($data);
        if($res){
            return api_return(1,'成功');
        }else{
            return api_return(2,'失败');
        }
    }
        //新闻动态   促销活动管理
    public function news_index(){
        $list=M('news')->select();
        foreach($list as $k=>$v){
            $list[$k]['create_time']=date('Y-m-d',$v['create_time']);
            if($v['img']){
                $list[$k]['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$v[img]")->getfield('path');
            }else{
                $list[$k]['img']='';
            }
        }
        if($list){
            return api_return(1,'成功',$list);
        }else{
            return api_return(1,'暂无信息',[]);
        }
    }
    //新闻详情
    public function news_detail(){
        $id=I('id');
        if(!$id){
            return api_return(2,'对象ID不能为空');
        }
        $info=M('news')->where("id=$id")->find();
        $info['create_time']=date('Y-m-d H:i:s',$info['create_time']);
        if($info['img']){
            $info['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$info[img]")->getfield('path');
        }else{
            $info['img']='';
        }
        M('news')->where("id=$id")->setInc('views',1);
        if($info){
            return api_return(1,'成功',$info);
        }else{
            return api_return(2,'暂无信息');
        }
    }
    //我的
    public function my(){
        $uid=I('uid');
        if($uid){
            $info=M('ucenter_member')->where("id=$uid")->field('img,id,username,type,point,state')->find();
            if(is_numeric($info['img'])){
                $info['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$info[img]")->getfield('path');
            }
            if($info['state']==1){
                $info['state_name']='经纪人';
            }elseif($info['state']==2){
                $info['state_name']='专职经纪人';
            }else{
                $info['state_name']='游客';
            }
            //总金额
            $listt=M('profit')->where("uid=$uid && type=1")->select();
            $price=0;
            foreach($listt as $k=>$v){
                $price+=$v['price'];
            }
            $info['profit']=$price;



            $info['uid']=$info['id'];
            $info['nickname']=$info['username'];
            unset($info['id']);
            unset($info['username']);
            return api_return(1,'成功',$info);
        }else{
            //收益
            $info['profit']=0;
            //我的优惠券
            $info['coupon']=0;
            //积分
            $info['point']=0;

            $info['state_name']='游客';
            $info['img']='';
            $info['type']='';
            $info['nickname']='';
            return api_return(1,'成功',$info);
        }
    }
    //基本信息回显
    public function essential_information(){
        $uid=I('uid');
        if(!$uid){
            return api_return(2,'对象ID不能为空');
        }
        $info=M('ucenter_member')->where("id=$uid")->find();
        if(is_numeric($info['img'])){
            $info['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$info[img]")->getfield('path');
        }
        if($info['province'] && $info['city'] && $info['area']){
            $info['region'][0]=M('area')->where("id=$info[province]")->getField('name');
            $info['region'][1]=M('area')->where("id=$info[city]")->getField('name');
            $info['region'][2]=M('area')->where("id=$info[area]")->getField('name');            
        }else{
            $info['region'][0]='湖南省';
            $info['region'][1]='长沙市';
            $info['region'][2]='岳麓区';           
        }
        $info['index']=$info['sex']-1;
        return api_return(1,'成功',$info);
    }

    //提交基本信息
    public function submit_information(){
        $uid=I('uid');
        if(!$uid){
            return api_return(2,'未登录');
        }
        $data['uid']=$uid;
        if(I('username')){
            $data['username']=I('username');
        }
        if(I('sex')){
            // file_put_contents('57.txt',I('sex'));
            $data['sex']=I('sex');
        }
        if(I('mobile')){
            $data['mobile']=I('mobile');
        }
        if(I('address')){
            $data['address']=I('address');
        }

        if(I('img')){
            $data['img']=I('img');
        }

        $province=$_POST['province'];
        $city=$_POST['city'];
        $area=$_POST['area'];

        if($province){
            $data['province']=M('area')->where("name='$province' && leveltype=1")->getField('id');
        }
        if($city){
            $data['city']=M('area')->where("name='$city' && leveltype=2")->getField('id');
        }
        if($area){
            $data['area']=M('area')->where("name='$area' && leveltype=3")->getField('id');
        }
        $res=M('ucenter_member')->where("id=$uid")->save($data);
        if($res){
            return api_return(1,'成功');
        }else{
            return api_return(2,'失败');
        }
        
    }
    //经纪人回显
    public function agent_echo(){
        $uid=I('uid');
        if(!$uid){
            return api_return(2,'未登录');
        }
        $info=M('agent')->where("uid=$uid")->find();
        if($info){
            $info['region'][0]=M('area')->where("id=$info[province]")->getField('name');
            $info['region'][1]=M('area')->where("id=$info[city]")->getField('name');
            $info['region'][2]=M('area')->where("id=$info[area]")->getField('name');
            $info['index']=$info['type'];
            $info['id_card_z']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$info[id_card_z]")->getfield('path');
            $info['id_card_f']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$info[id_card_f]")->getfield('path');
            if($info['status']==1){
                $info['state']=1;
            }else{
                $info['state']=2;
            }
            
            return api_return(1,'成功',$info);

        }else{
            $info['region'][0]='湖南省';
            $info['region'][1]='长沙市';
            $info['region'][2]='岳麓区';
            $info['index']=0;
            $info['id_card_z']='../../images/img_bg9.jpg';
            $info['id_card_f']='../../images/img_bg9.jpg';
            $info['state']=2;
            return api_return(1,'暂无信息',$info);
        }
    }
    //经纪人认证
    public function agent_authentication(){
        $uid=I('uid');
        if(!$uid){
            return api_return(2,'未登录');
        }
        $data['uid']=$uid;
        // $data['email']=I('email');
        // if(!$data['email']){
        //     return api_return(2,'邮箱不能为空');
        // }
        if(I('email')){
            $data['email']=I('email');
        }
        // $data['occupation']=I('occupation');
        // if(!$data['occupation']){
        //     return api_return(2,'职业不能为空');
        // }
        if(I('occupation')){
            $data['occupation']=I('occupation');
        }
        // $data['id_number']=I('id_number');
        // if(!$data['id_number']){
        //     return api_return(2,'身份证号码不能为空');
        // }
        if(I('id_number')){
            $data['id_number']=I('id_number');
        }       
        // $data['id_card_z']=I('id_card_z');
        // if(!$data['id_card_z']){
        //     return api_return(2,'身份证正面不能为空');
        // }
        if(I('id_card_z')){
            $data['id_card_z']=I('id_card_z');
        }  
        // $data['id_card_f']=I('id_card_f');
        // if(!$data['id_card_f']){
        //     return api_return(2,'身份证反面不能为空');
        // }
        if(I('id_card_f')){
            $data['id_card_f']=I('id_card_f');
        }  
        // $data['address']=I('address');
        // if(!$data['address']){
        //     return api_return(2,'地址不能为空');
        // }
        if(I('address')){
            $data['address']=I('address');
        } 
        // $data['company']=I('company');
        // if(!$data['company']){
        //     return api_return(2,'单位不能为空');
        // }
        if(I('company')){
            $data['company']=I('company');
        } 
        $province=$_POST['province'];
        $city=$_POST['city'];
        $area=$_POST['area'];

        if($province){
            $data['province']=M('area')->where("name='$province' && leveltype=1")->getField('id');
        }
        if($city){
            $data['city']=M('area')->where("name='$city' && leveltype=2")->getField('id');
        }
        if($area){
            $data['area']=M('area')->where("name='$area' && leveltype=3")->getField('id');
        }


        $data['type']=$_POST['type'];
        // if(I('type')){
        //     $data['type']=$_POST['type'];
        // }
        $cz=M('agent')->where("uid=$uid")->find();

        //机构后端添加经纪人有姓名 手机号
        $data['name']=M('ucenter_member')->where("id=$uid")->getField('username');
        $data['mobile']=M('ucenter_member')->where("id=$uid")->getField('mobile');
        $data['img']=M('ucenter_member')->where("id=$uid")->getField('img');
        if($cz){
            $data['create_time']=time();
            $res=M('agent')->where("uid=$uid")->save($data); 
        }else{
            $data['create_time']=time();
            $res=M('agent')->add($data); 
        }
          
        if($res){
            M('ucenter_member')->where("id=$uid")->save(['type'=>1]);
            return api_return(1,'成功');
        }else{
            return api_return(2,'失败');
        }                                     
    }
    //我的页面回显
    public function my_echo(){
        $uid=I('uid');
        if($uid){
            $data['status']=M('agent')->where("uid=$uid")->getField('status');
            if($data['status']==1){
                $type=M('agent')->where("uid=$uid")->getField('type');
                if($type==1){
                    $data['type']='专职经纪人';
                }elseif($type==2){
                    $data['type']='渠道级经纪人';
                }else{
                    $data['type']='经济人';
                }
            }
        }else{

        }
    }
    //我的收益
    public function my_profit(){
        $uid=I('uid');
        if(!$uid){
            return api_return(2,'未登录');
        }

        $list=M('profit')->where("uid=$uid")->order('id desc')->select();
        foreach($list as $k=>$v){
            if($v['status']==1){
                $list[$k]['status_name']='审核中';
            }elseif($v['status']==2){
                $list[$k]['status_name']='成功';
            }
            if($v['reason']==1){
                $list[$k]['reason_name']='申请提现';
            }elseif($v['reason']==2){
                $list[$k]['reason_name']='成功推荐客户';
            }
            $list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
        }

        //总金额
        $listt=M('profit')->where("uid=$uid && type=1")->select();
        $price=0;
        foreach($listt as $k=>$v){
            $price+=$v['price'];
        }


        //可提现金额
        



        //审核中
        $list2=M('profit')->where("uid=$uid && type=2 && status=1")->select();
        $price2=0;
        foreach($list2 as $k=>$v){
            $price2+=$v['price'];
        }
        //已提现   审核成功才算已提现
        $list3=M('profit')->where("uid=$uid && type=2 && status=2")->select();
        $price3=0;
        foreach($list3 as $k=>$v){
            $price3+=$v['price'];
        }

        //待提现   我的理解就是可提现
        $price1=$price-$price2-$price3;

        $list?$data['list']=$list:[];
        $data['price1']=$price1;
        $data['price2']=$price2;
        $data['price3']=$price3;
        return api_return(1,'成功',$data);
    }
    //提交提现页面
    public function withdrawal_echo(){
        $uid=I('uid');
        if(!$uid){
            return api_return(2,'未登录');
        }
        $data['uid']=$uid;
        $data['create_time']=time();
        $data['price']=I('price');
        if(!$data['price']){
            return api_return(2,'价格不能为空');
        }

        //总金额
        $listt=M('profit')->where("uid=$uid && type=1")->select();
        $price=0;
        foreach($listt as $k=>$v){
            $price+=$v['price'];
        }

        //审核中
        $list2=M('profit')->where("uid=$uid && type=2 && status=1")->select();
        $price2=0;
        foreach($list2 as $k=>$v){
            $price2+=$v['price'];
        }
        //已提现   审核成功才算已提现
        $list3=M('profit')->where("uid=$uid && type=2 && status=2")->select();
        $price3=0;
        foreach($list3 as $k=>$v){
            $price3+=$v['price'];
        }

        //待提现   我的理解就是可提现
        $price1=$price-$price2-$price3;

        if($price1<$data['price']){
            return api_return(2,'提现超额');
        }
        $data['name']=I('name');
        if(!$data['name']){
            return api_return(2,'姓名不能为空');
        }
        $data['img']=I('img');
        if(!$data['img']){
            return api_return(2,'收款码不能为空');
        }
        $data['type']=2;
        $data['reason']=1;
        $res=M('profit')->add($data);
        if($res){
            return api_return(1,'成功');
        }else{
            return api_return(2,'失败');
        }
        
    }
    //我的优惠券  机构后台都没这些东西

    //我的积分
    public function my_point(){
        $uid=I('uid');
        if(!$uid){
            return api_return(2,'会员ID不能为空');
        }
        $type=I('type');
        if($type==1 || !$type){
            $list=M('point')->where("uid=$uid && type=1")->select();
        }else{
            $list=M('point')->where("uid=$uid && type=2")->select();
        }
        foreach($list as $k=>$v){
            $list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
            if($v['reason']==1){
                $list[$k]['reason_name']='注册';
            }elseif($v['reason']==2){
                $list[$k]['reason_name']='分享好友';
            }elseif($v['reason']==3){
                $list[$k]['reason_name']='预约';
            }elseif($v['reason']==4){
                $list[$k]['reason_name']='推荐用户';
            }elseif($v['reason']==5){
                $list[$k]['reason_name']='购买商品';
            }
        }
        $count=M('ucenter_member')->where("id=$uid")->getField('point');
        $data['list']=$list;
        $data['count']=$count;
        $data['content']=M('point_rule')->getField('content');
        return api_return(1,'成功',$data);
    }
    //积分规则
    public function point_rule(){
        $content=M('point_rule')->getField('content');
        return api_return(1,'成功',$content);
    }


    //我推荐的客户
    public function my_customer(){
        $uid=I('uid');
        if(!$uid){
            return api_return(2,'未登录');
        }
        $type=I('type');
        if($type==1){
            //所有
        }elseif($type==2){
            //已推荐
            $map['status']=1;
        }elseif($type==3){
            //已到访
            $map['status']=2;
        }elseif($type==4){
            //已签约
            $map['status']=5;
        }elseif($type==5){
            //已失效
            $map['status']=6;
        }
        $map['references']=$uid;
        $list=M('user')->where($map)->select();
        if($list){
            foreach($list as $k=>$v){
                $list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
                if($v['status']==1){
                    $list[$k]['status_name']='已推荐';
                }elseif($v['status']==2){
                    $list[$k]['status_name']='已上访';
                }elseif($v['status']==3){
                    $list[$k]['status_name']='已认筹';
                }elseif($v['status']==4){
                    $list[$k]['status_name']='已认购';
                }elseif($v['status']==5){
                    $list[$k]['status_name']='已签约';
                }elseif($v['status']==6){
                    $list[$k]['status_name']='已失效';
                }
                if($v['sex']==1){
                    $list[$k]['sex_name']='男';
                }else{
                    $list[$k]['sex_name']='女';
                }
            }
        }else{
            $list=[];
        }

        return api_return(1,'成功',$list);
    }

    //积分商城
    public function points_mall(){
        $uid=I('uid');
        if($uid){
            //我的积分  积分以前设计是放在员工表，做用户端的时候感觉不对
            $data['point']=M('ucenter_member')->where("id=$uid")->getfield('point');
        }
        //上架
        $list=M('good')->where('status=1')->select();
        foreach($list as $k=>$v){
            if($v['img']){
               $list[$k]['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$v[img]")->getfield('path'); 
            }else{
                $list[$k]['img']='';
            }
            $list[$k]['floor_name']=M('floor')->where("id=$v[floor_id]")->getField('name');
        }
        $list?$data['list']=$list:$data['list']=[];
        return api_return(1,'成功',$data);
    }
    //兑换记录
    public function exchange_record(){
        $uid=I('uid');
        if(!$uid){
            return api_return(2,'未登录');
        }
        $type=I('type');

        if($type==1){

        }elseif($type==2){
            $map['status']=1;
        }elseif($type==3){
            $map['status']=2;
        }elseif($type==4){
            $map['status']=3;
        }
        $map['uid']=$uid;

        $list=M('order')->where($map)->order('id desc')->select();
        foreach($list as $k=>$v){
            $img=M('good')->where("id=$v[good_id]")->getfield('img');
            if($img){
                $list[$k]['good_img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$img")->getfield('path');
            }else{
                $list[$k]['good_img']='';
            }
            
            if($v['status']==1){
                $list[$k]['status_name']='待审核';
            }elseif($v['status']==2){
                $list[$k]['status_name']='已审核';
            }elseif($v['status']==3){
                $list[$k]['status_name']='已发货';
            }elseif($v['status']==4){
                $list[$k]['status_name']='已自提';
            }elseif($v['status']==5){
                $list[$k]['status_name']='已完成';
            }elseif($v['status']==6){
                $list[$k]['status_name']='已核销';
            }


            $list[$k]['good_name']=M('good')->where("id=$v[good_id]")->getfield('name');
            $list[$k]['floor_name']=M('floor')->where("id=$v[floor_id]")->getfield('name');
        }
        return api_return(1,'成功',$list);
    }
    //兑换记录详情
    public function order_detail(){
        $id=I('id');
        //这个是订单ID
        if(!$id){
            return api_return(2,'对象ID不能为空');
        }
        $info=M('order')->where("id=$id")->find();
        $img=M('good')->where("id=$info[good_id]")->getfield('img');
        $info['good_name']=M('good')->where("id=$info[good_id]")->getfield('name');
        if($img){
            $info['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$img")->getfield('path');
        }else{
            $info['img']='';
        }

        if($info['status']==1){
            $info['status_name']='待审核';
        }elseif($info['status']==2){
            $info['status_name']='已审核';
        }elseif($info['status']==3){
            $info['status_name']='已发货';
        }elseif($info['status']==4){
            $info['status_name']='已自提';
        }elseif($info['status']==5){
            $info['status_name']='已完成';
        }elseif($info['status']==6){
            $info['status_name']='已核销';
        }


        $info['floor_name']=M('floor')->where("id=$info[floor_id]")->getfield('name');
        return api_return(1,'成功',$info);
    }
    //兑换详情  上面是兑换完了，这个是准备兑换
    public function exchange_detail(){
        $id=I('id');
        //这个是商品ID
        if(!$id){
            return api_return(2,'对象ID不能为空');
        }
        $uid=I('uid');
        file_put_contents('57.txt', $uid);
        //这个是商品ID
        if(!$uid){
            return api_return(2,'未登录');
        }
        $info=M('good')->where("id=$id")->find();
        if($info['img']){
            $info['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$info[img]")->getfield('path');
        }else{
            $info['img']='';
        }

        $info['floor_name']=M('floor')->where("id=$info[floor_id]")->getfield('name');

        $info['my_point']=M('ucenter_member')->where("id=$uid")->getField('point');
        $address=M('ucenter_member')->where("id=$uid")->field('province,city,area,address')->find();
        if($address['province'] && $address['city'] && $address['area'] && $address['address']){
            $pname=M('area')->where("id=$address[province]")->getField('name');
            $pcity=M('area')->where("id=$address[city]")->getField('name');
            $parea=M('area')->where("id=$address[area]")->getField('name');  
            $info['address']=$pname.$pcity.$parea.$address['address'];
        }else{
            $info['address']='';
        }
        
        return api_return(1,'成功',$info);
    }
    //接收兑换页面的参数，生成订单
    public function order(){
        $data['uid']=I('uid');
        if(!$data['uid']){
            return api_return(2,'未登录');
        }
        // $data['get_name']=I('name');
        // if(!$data['get_name']){
        //     return api_return(2,'收件人姓名不能为空');
        // }
        // $data['get_mobile']=I('get_mobile');
        // if(!$data['get_mobile']){ 
        //     return api_return(2,'手机号码不能为空');
        // }
        // $data['address']=I('address');
        // if(!$data['address']){
        //     return api_return(2,'地址不能为空');
        // }

        $data['get_name']=M('ucenter_member')->where("id=$data[uid]")->getField('username');
        $data['get_mobile']=M('ucenter_member')->where("id=$data[uid]")->getField('mobile');


        $address=M('ucenter_member')->where("id=$data[uid]")->field('province,city,area,address')->find();

        $pname=M('area')->where("id=$address[province]")->getField('name');
        $pcity=M('area')->where("id=$address[city]")->getField('name');
        $parea=M('area')->where("id=$address[area]")->getField('name');  
        $data['address']=$pname.$pcity.$parea.$address['address'];
// print_r($data);die;



        $data['good_id']=I('good_id');
        if(!$data['good_id']){
            return api_return(2,'商品ID不能为空');
        }
        $data['point']=M('good')->where("id=$data[good_id]")->getfield('point');
        $data['remark']=I('remark');
        $data['order_sn']="lp".time().$data['uid'];
        $data['floor_id']=M('good')->where("id=$data[good_id]")->getfield('floor_id');
        $data['me_id']=M('floor')->where("id=$data[floor_id]")->getField('me_id');
        $data['create_time']=time();
        $res=M('order')->add($data);
        if($res){
            //用户减少积分
            M('ucenter_member')->where("id=$data[uid]")->setDec('point',$data['point']);

            $add_data['uid']=$data['uid'];
            $add_data['type']=2;
            $add_data['reason']=5;
            $add_data['count']=$data['point'];
            $add_data['create_time']=time();
            M('point')->add($add_data);
            return api_return(1,'成功');
        }else{
            return api_return(2,'失败');
        }
    }
    //商品详情
    public function good_info(){
        $id=I('id');
        if(!$id){
            return api_return(2,'对象ID不能为空');
        }
        $info=M('good')->where("id=$id")->find();
        // if($info['img']){
        //     $info['img']=M('picture')->where("id=$info[img]")->getfield('path');
        // }else{
        //     $info['img']='';
        // }

        if($info['imgs']);
        $imgs=explode(',',$info['imgs']);
        unset($info['imgs']);
        // file_put_contents('123.txt', $imgs);
        foreach($imgs as $kk=>$vv){
            $info['imgs'][$kk]="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$vv")->getfield('path');
        }
        $info['floor_name']=M('floor')->where("id=$info[floor_id]")->getField('name');
        return api_return(1,'成功',$info);
    }
    //我的销售顾问
    public function my_consultant(){
        $uid=I('uid');
        if(!$uid){
            return api_return(2,'未登录');
        }
        //自己预约了跟别人推荐的
        $list=M('user')->where("uid=$uid || b_uid=$uid")->select();
        foreach($list as $k=>$v){
            $img=M('department_staff')->where("id=$v[adviser]")->getfield('img');
            $list[$k]['adviser_info']=M('department_staff')->where("id=$v[adviser]")->find();
            if($img){
                $list[$k]['adviser_info']['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$img")->getfield('path');
            }else{
                $list[$k]['adviser_info']['img']='';
            }
        }
        return api_return(1,'成功',$list);
    }
    //我的关注
    public function my_follow(){
        $uid=I('uid');
        if(!$uid){
            return api_return(2,'未登录');
        }
        $list=M('follow_floor')->where("uid=$uid")->select();
        foreach($list as $k=>$v){
            $list[$k]['floor_info']=M('floor')->where("id=$v[floor_id]")->find();
            $img=M('floor')->where("id=$v[floor_id]")->getfield('img');
            if($img){
                $list[$k]['floor_info']['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$img")->getfield('path');
            }else{
                $list[$k]['floor_info']['img']='';
            }
        }
        return api_return(1,'成功',$list);
    }
    //我的活动
    public function my_activity(){
        $uid=I('uid');
        if(!$uid){
            return api_return(2,'未登录');
        }
        $list=M('sign_up')->where("uid=$uid")->select();
        foreach($list as $k=>$v){
            $list[$k]['activity_info']=M('activity')->where("id=$v[activity_id]")->find();
            $img=M('activity')->where("id=$v[activity_id]")->getfield('img');
            if($img){
                $list[$k]['activity_info']['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$img")->getfield('path');
            }else{
                $list[$k]['activity_info']['img']='';
            }
            //活动时间
            $list[$k]['activity_info']['start_time']=date('Y-m-d',$list[$k]['activity_info']['start_time']);
            $list[$k]['activity_info']['end_time']=date('Y-m-d',$list[$k]['activity_info']['end_time']);
            //总共多少人报名
            $list[$k]['count']=M('sign_up')->where("activity_id=$v[activity_id]")->count();
        }
        return api_return(1,'成功',$list);
    }
    //百问百答
    public function answer(){
        $list=M('answer')->select();
        return api_return(1,'成功',$list);
    }

            //单图
    public function up_img(){
        if (empty($_FILES) ){
            return api_return(2,'请上传图片');
        }else{
            $file = $_FILES['file'];
            //             $filee='1025.txt';
            // file_put_contents($filee,$file['name']);
            if (!is_dir("Uploads/Picture/" . date("Y-m-d"))) {
                mkdir("Uploads/Picture/" . date("Y-m-d"));
            }
            if (is_uploaded_file($file['tmp_name'])) {
                $str = stristr($file['name'], '.');
                $str=substr($str,1);
                $avatar_path = "Uploads/Picture/" . date("Y-m-d") . "/" . time() . rand(1000,9999) . '.' . $str;
                if(copy($file['tmp_name'],$avatar_path)){
                    $game_path = str_replace('Uploads/Picture/','game/headimage/',$avatar_path);
                    copy($file['tmp_name'],$game_path);
                    $cover['path'] = "/".$avatar_path;
                    $cover['status'] = 1;
                    $cover['create_time'] = time();
                    $re = M('picture')->add($cover);
                    if($re){
                        $img=get_host() . get_cover($re, "path");
                        $hh['imgurl']=$img;
                        $hh['imgid']=$re;
                        return api_return(1,'图片上传成功',$hh); 
                    }else{
                        return api_return(2,'图片上传失败');
                    }
                }else {
                    return api_return(2,'图片上传失败');
                }
            }
        }
    }
    //机构端添加用户 先到user表，再添加到ucenter_member表，然后用户登录，获取手机号码，判断他是否在用户表里(没添加到用户表了，后天添加用户意思就算添加潜在客户)


    public function getWxUserInfo(){
    $input = $_POST;
    // file_put_contents('429.txt', $input);exit;
    $code = $input['code'];
    $encryptedData = $input['encryptedData'];
    $iv = $input['iv'];
    if(empty($code)) return api_return(2,'code不能为空');
    if(empty($encryptedData)) return api_return(2,'encryptedData不能为空');
    if(empty($iv)) return api_return(2,'iv不能为空');
    
    $appid = 'wx7c2400f9d4f53780';
    $secret = '9da611b0bec45558aeb40f32f9e48b83';
    $grant_type='authorization_code';
    $url = 'https://api.weixin.qq.com/sns/jscode2session?';
    $param = [
        'appid' => $appid,
        'secret' => $secret,
        'js_code' => $code,
        'grant_type' => $grant_type
    ];
    $param_str = http_build_query($param);
    // $url .= $param_str;
    // $user_data = json_decode(file_get_contents($url));
    // return api_return(1,'登录成功',$url);
    // $session_key = $this->define_str_replace($user_data->session_key);
    // $data="";
    // vendor('wxApp.wxBizDataCrypt');//引入微信解密文件
    // $wxBizDataCrypt = new \WXBizDataCrypt($appid,$session_key);
    // $errCode = $wxBizDataCrypt->decryptData($encryptedData,$iv,$data);
    // if($errCode == 0){
    // //用户信息
    //     $userinfo = json_decode($data,true);
    //     return api_return(1,'登录成功',$userinfo);
    // //业务逻辑处理
    // }else{
    //     return api_return(2,'登录失败，请稍后重试，错误码：'.$errCode);
    // }





        $url = $url . $param_str;
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);  // 从证书中检查SSL加密算法是否存在
        $user_data = curl_exec($curl);     //返回api的json对象
        //关闭URL请求
        curl_close($curl);

        $member_data=json_decode($user_data,true);
        $openid=$member_data['openid'];
        $user_data = json_decode($user_data);
        // return api_return(2,'失败',$user_data);
        $session_key = $this->define_str_replace($user_data->session_key);
        $session_key=stripslashes($session_key);
        // return api_return(2,'失败',$session_key);
        $user_info="";
        vendor('wxApp.wxBizDataCrypt');//引入微信解密文件
        $wxBizDataCrypt = new \WXBizDataCrypt($appid,$session_key);

        $errCode=$wxBizDataCrypt->decryptData($encryptedData,$iv,$user_info);
        // return api_return(2,'失败',$errCode);
        if($errCode!==0){
            // $data['result'] = 2;
            // $data['message'] = $errCode;
            // $this->ajaxReturn($data);
            return api_return(2,'失败',$errCode);
        }
        $user_info=json_decode($user_info,true);
        $user_info['session_key']=$session_key;
        $user_info['openid']=$openid;
        return api_return(1,'成功',$user_info);
}

function define_str_replace($data)

{

    return str_replace(' ','+',$data);

}

public function phone(){
    header("Content-Type:text/html;charset=utf8"); 
    header("Access-Control-Allow-Origin: *"); //解决跨域
    header('Access-Control-Allow-Methods:POST');// 响应类型  
    header('Access-Control-Allow-Headers:*'); // 响应头设置 
 
    $appid = 'wxf4de8445d320ba04';

    $sessionKey = $_POST['session_key'];
    if(!$sessionKey){
        return api_return(2,'sessionKey不能为空');
    }
    $encryptedData = $_POST['encryptedData'];
    if(!$encryptedData){
        return api_return(2,'encryptedData不能为空');
    }
    $iv = $_POST['iv'];
    if(!$iv){
        return api_return(2,'iv不能为空');
    }
    // include_once "wxBizDataCrypt.php";
    vendor('wxApp.wxBizDataCrypt');//引入微信解密文件
    $data = '';
    $pc = new \WXBizDataCrypt($appid, $sessionKey);
    $errCode = $pc->decryptData($encryptedData, $iv, $data );
 
    if ($errCode == 0) {
        $bb=json_decode($data,true);

        // $bb['openid']=$bb['watermark']['appid'];
        $bb['openid']=$_POST['openID'];

        $map['mobile']=$bb['phoneNumber'];
        $info=M('ucenter_member')->where($map)->find();
        if($info){
            $bb['uid']=$info['id'];
            M('ucenter_member')->where("id=$info[id]")->save(['openid'=>$bb['openid']]);
        }else{
            $infoo['mobile']=$bb['phoneNumber'];
            $infoo['username']=$bb['phoneNumber'];
            $infoo['reg_time']=time();
            // $infoo['openid']=$bb['openid'];
            $bb['openid']=$_POST['openID'];
            $infoo['email']=$bb['phoneNumber'].'@'.$bb['phoneNumber'].'.com';
            $res=M('ucenter_member')->add($infoo);
            if($res){
                $ad['uid']=$res;
                $ad['nickname']=$bb['phoneNumber'];
                $ad['reg_time']=time();
                $ad['phone']=$bb['phoneNumber'];
                $result=M('member')->add($ad);
                $bb['uid']=$res;
                return api_return(1,'成功',$bb);
            }else{
                $bb['uid']='';
                return api_return(2,'失败',$bb);
            }

        }

        return api_return(1,'成功',$bb);
    } else {
        return api_return(2,'失败',$errCode);
    }

}


//获取微信小程序二维码
public function qr_code(){
    $uid=I('uid');
    $appid = 'wx7c2400f9d4f53780';
    $secret = '9da611b0bec45558aeb40f32f9e48b83';
    $access_token = json_decode(file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret),true)['access_token'];
    $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$access_token";
    //$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=45_Satg4G5S6wPvFGkQtvqWg9f8Ii6RB_gPpOgo43L779f9TaIum232xm2fAMSqVbsEDAqV7ylARXcHHRJkoezOCxTeYXchetxPfnuFUoDyHxnj5lsNrun-y_IsZeehxIL5MxhTqF0raV1piHTxKNReAEARUY";
    $ch = curl_init();
    $data = json_encode(['scene' => '&id='.$uid]);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 'image/gif');
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data)
    ));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //如果需要将结果直接返回到变量里，那加上这句。
    $res = curl_exec($ch);
    $data1['img_src'] = 'data:image/jpeg;base64,'.base64_encode($res);
    $data1['nickname']=M('ucenter_member')->where("id=$uid")->getField('username');
    $img=M('ucenter_member')->where("id=$uid")->getField('img');
    if(is_numeric($img)){
        $data1['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$img")->getfield('path');
    }else{
         $data1['img']=$img;
    }
    return api_return(1,'成功',$data1);
}


//小程序登录注册
public function login(){
    $data['sex']=I('sex');
    $data['img']=I('img');
    $data['username']=I('nickname');
    $data['openid']=I('openid');
    $map['openid']=I('openid');

    $info=M('ucenter_member')->where($map)->field('img,id,username,type,point')->find();

    if(!$info){
        // file_put_contents('431.txt',$data['username']);
        $res=M('ucenter_member')->add($data);
        if($res){
            $add_data['nickname']=I('nickname');
            $add_data['sex']=I('sex');
            $add_data['uid']=$res;
            M('member')->add($add_data);

            //判断是否有推广ID，有就说明是通过别人得二维码进来得，有积分奖励
            $promotion_id=I('promotion_id');
            if($promotion_id){
                $add_point['uid']=$promotion_id;
                $add_point['type']=1;
                $add_point['count']=10;//后面应该要设置，现在先写死
                $add_point['reason']=6;
                $add_point['create_time']=time();
                M('point')->add($add_point); 
            }

            //注册也得积分
            $add_point1['uid']=$res;
            $add_point1['type']=1;
            $add_point1['count']=5;//后面应该要设置，现在先写死
            $add_point1['reason']=1;
            $add_point1['create_time']=time();
            M('point')->add($add_point); 


            $info['img']=I('img');
            $info['uid']=$res;
            $info['nickname']=I('nickname');
            $info['type']=0;
            $info['point']=0;
            return api_return(1,'成功',$info);
        }else{
            return api_return(2,'失败');
        }
    }else{

        if(is_numeric($info['img'])){
            $info['img']="http://" . $_SERVER['HTTP_HOST'] .M('picture')->where("id=$info[img]")->getfield('path');
        }
        $info['uid']=$info['id'];
        $info['nickname']=$info['username'];
        unset($info['id']);
        unset($info['username']);
        return api_return(1,'成功',$info);
    }
}
//获取省份数据
public function province_data(){
    $condition['me_id']=I('me_id');
    $province='北京';
    $condition['province']=array('like',"%$province%");
    $data['count']=M('floor')->where($condition)->count();
    $province='天津';
    $condition['province']=array('like',"%$province%");
    $data['count1']=M('floor')->where($condition)->count();
    $province='上海';
    $condition['province']=array('like',"%$province%");
    $data['count2']=M('floor')->where($condition)->count();
    $province='重庆';
    $condition['province']=array('like',"%$province%");
    $data['count3']=M('floor')->where($condition)->count();
    $province='河北';
    $condition['province']=array('like',"%$province%");
    $data['count4']=M('floor')->where($condition)->count();
    $province='河南';
    $condition['province']=array('like',"%$province%");
    $data['count5']=M('floor')->where($condition)->count();
    $province='云南';
    $condition['province']=array('like',"%$province%");
    $data['count6']=M('floor')->where($condition)->count();
    $province='辽宁';
    $condition['province']=array('like',"%$province%");
    $data['count7']=M('floor')->where($condition)->count();
    $province='黑龙江';
    $condition['province']=array('like',"%$province%");
    $data['count8']=M('floor')->where($condition)->count();
    $province='湖南';
    $condition['province']=array('like',"%$province%");
    $data['count9']=M('floor')->where($condition)->count();
    $province='安徽';
    $condition['province']=array('like',"%$province%");
    $data['count10']=M('floor')->where($condition)->count();
    $province='山东';
    $condition['province']=array('like',"%$province%");
    $data['count11']=M('floor')->where($condition)->count();
    $province='新建';
    $condition['province']=array('like',"%$province%");
    $data['count12']=M('floor')->where($condition)->count();
    $province='江苏';
    $condition['province']=array('like',"%$province%");
    $data['count13']=M('floor')->where($condition)->count();
    $province='浙江';
    $condition['province']=array('like',"%$province%");
    $data['count14']=M('floor')->where($condition)->count();
    $province='江西';
    $condition['province']=array('like',"%$province%");
    $data['count15']=M('floor')->where($condition)->count();
    $province='湖北';
    $condition['province']=array('like',"%$province%");
    $data['count16']=M('floor')->where($condition)->count();
    $province='广西';
    $condition['province']=array('like',"%$province%");
    $data['count17']=M('floor')->where($condition)->count();
    $province='甘肃';
    $condition['province']=array('like',"%$province%");
    $data['count18']=M('floor')->where($condition)->count();
    $province='山西';
    $condition['province']=array('like',"%$province%");
    $data['count19']=M('floor')->where($condition)->count();
    $province='内蒙古';
    $condition['province']=array('like',"%$province%");
    $data['count20']=M('floor')->where($condition)->count();
    $province='陕西';
    $condition['province']=array('like',"%$province%");
    $data['count21']=M('floor')->where($condition)->count();
    $province='吉林';
    $condition['province']=array('like',"%$province%");
    $data['count22']=M('floor')->where($condition)->count();
    $province='福建';
    $condition['province']=array('like',"%$province%");
    $data['count23']=M('floor')->where($condition)->count();
    $province='贵州';
    $condition['province']=array('like',"%$province%");
    $data['count24']=M('floor')->where($condition)->count();
    $province='广东';
    $condition['province']=array('like',"%$province%");
    $data['count25']=M('floor')->where($condition)->count();
    $province='青海';
    $condition['province']=array('like',"%$province%");
    $data['count26']=M('floor')->where($condition)->count();
    $province='西藏';
    $condition['province']=array('like',"%$province%");
    $data['count27']=M('floor')->where($condition)->count();
    $province='四川';
    $condition['province']=array('like',"%$province%");
    $data['count28']=M('floor')->where($condition)->count();
    $province='宁夏';
    $condition['province']=array('like',"%$province%");
    $data['count29']=M('floor')->where($condition)->count();
    $province='海南';
    $condition['province']=array('like',"%$province%");
    $data['count30']=M('floor')->where($condition)->count();
    $province='台湾';
    $condition['province']=array('like',"%$province%");
    $data['count31']=M('floor')->where($condition)->count();
    $province='香港';
    $condition['province']=array('like',"%$province%");
    $data['count32']=M('floor')->where($condition)->count();
    $province='澳门';
    $condition['province']=array('like',"%$province%");
    $data['count33']=M('floor')->where($condition)->count();
    return api_return(1,'成功',$data);
}


public function phone1(){
    header("Content-Type:text/html;charset=utf8"); 
    header("Access-Control-Allow-Origin: *"); //解决跨域
    header('Access-Control-Allow-Methods:POST');// 响应类型  
    header('Access-Control-Allow-Headers:*'); // 响应头设置 
 
    $appid = 'wx7c2400f9d4f53780';

    $sessionKey = $_POST['session_key'];
    if(!$sessionKey){
        return api_return(2,'sessionKey不能为空');
    }
    $encryptedData = $_POST['encryptedData'];
    if(!$encryptedData){
        return api_return(2,'encryptedData不能为空');
    }
    $iv = $_POST['iv'];
    if(!$iv){
        return api_return(2,'iv不能为空');
    }
    // include_once "wxBizDataCrypt.php";
    vendor('wxApp.wxBizDataCrypt');//引入微信解密文件
    $data = '';
    $pc = new \WXBizDataCrypt($appid, $sessionKey);
    $errCode = $pc->decryptData($encryptedData, $iv, $data );
 
    if ($errCode == 0) {
        $bb=json_decode($data,true);

        $info['mobile']=$bb['phoneNumber'];

        return api_return(1,'成功',$info);
    } else {
        return api_return(2,'失败',$errCode);
    }

}

}